package Product;

import dao.ReviewDao;
import entity.Review;
import service.ReviewService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/productDetail")
public class ProductDetailServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 从请求中获取pid
        int pid = Integer.parseInt(request.getParameter("pid"));
        // 使用ProductDao获取产品信息
        ProductDao productDao = new ProductDao();
        HashMap<String, String> productInfo = productDao.getByPid(pid);
        ReviewService reviewService = new ReviewService();
        List<Review> reviews = reviewService.queryReviewsByProductId(pid);
        // 将产品信息放入请求属性中，以便在视图中访问
        request.setAttribute("productInfo", productInfo);
        request.setAttribute("reviews",reviews);
        // 转发请求到JSP页面，productDetail.jsp
        request.getRequestDispatcher("/product").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("uid"));

        int productId = Integer.parseInt(request.getParameter("pid"));
        ProductDao productDao = new ProductDao();
        productDao.addFavorite(userId, productId);
        System.out.println(productId);
        response.sendRedirect("/productDetail?pid="+productId);
    }
}